package com.byxy.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.annotations.One;

import com.byxy.entity.Tip;

public interface TipDao {
	@Results(id="tipMap" , value={
		@Result(column="tip_id",property="tipId",id=true),
		@Result(column="user_id",property="user",one=@One(select="com.byxy.dao.UserDao.getById",fetchType=FetchType.EAGER)),
		@Result(column="tab_id",property="tab",one=@One(select="com.byxy.dao.TabDao.getById",fetchType=FetchType.EAGER)),
		@Result(column="tip_content",property="tipContent"),
		@Result(column="tip_title",property="tipTitle"),
		@Result(column="tip_publishTime",property="tipPublishTime"),
		@Result(column="tip_modifyTime",property="tipModifyTime"),
		@Result(column="tip_click",property="tipClick"),
		@Result(column="tip_isDeleted",property="tipIsdeleted"),
		@Result(column="tip_isKnot",property="tipIsKnot"),
		@Result(column="tip_replies",property="tipReplies")
	})
	@Select("select * from tip order by tip_modifyTime desc")
	List<Tip> find();
	
	@ResultMap("tipMap")
	@Select("select * from tip")
	List<Tip> findByPage(@Param("pageNum")int pageNum,@Param("pageSize")int pageSize);
	
	@Insert("insert into tip(tip_content,tip_title,tip_publishTime,tip_modifyTime,tip_click,tip_isDeleted,tip_isKnot,tip_replies) "
			+ "values(#{tipContent},#{tipTitle},#{tipPublishTime},#{tipModifyTime},#{tipClick},#{tipIsdeleted},#{tipIsKnot},#{tipReplies})")
	void add(Tip tip);
	
	@ResultMap("tipMap")
	@Select("select * from tip where tip_id=#{tipId}")
	Tip findById(Integer tipId);
	
	@Update("update tip set tip_isKnot=1 where tip_id=#{tipId}")
	void knotTip(Integer tipId);
}
